package com.jiezuo.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.jiezuo.auth.entity.UserEntry;
import com.jiezuo.auth.service.UserEntryService;
import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.enums.ResultEnums;
import com.jiezuo.common.util.PageUtil;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.util.VerifyUtil;
import com.jiezuo.common.vo.PageVo;
import com.jiezuo.common.vo.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 用户自定义列表
 * @date 2023/11/22 16:39
 **/
@Slf4j
@Transactional
@RequestMapping("/userEntry")
@RestController
//用户自定义列表", tags = {"用户自定义列表接口"})
public class UserEntryController extends BaseController {

    @Autowired
    private UserEntryService service;

    //查询带分页列表", notes = "查询带分页列表")
    @GetMapping("/findPage")
    public Result<IPage<UserEntry>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute UserEntry userEntry) {
        IPage<UserEntry> page = service.findPage(PageUtil.initMpPage(pageVo), userEntry);
        return new ResultUtil<IPage<UserEntry>>().setData(page);
    }

    //查询不带分页列表", notes = "查询不带分页列表")
    @GetMapping("/findList")
    public Result<List<UserEntry>> findList(@ModelAttribute UserEntry userEntry) {
        List<UserEntry> list = service.findList(userEntry);
        return new ResultUtil<List<UserEntry>>().setData(list);
    }

    //通过ID查询", notes = "通过ID查询")
    @GetMapping("/findById")
    public Result<UserEntry> findById(String id) {
        UserEntry userEntry = service.findById(id);
        return new ResultUtil<UserEntry>().setData(userEntry);
    }

    //添加", notes = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody UserEntry userEntry) {
        VerifyUtil.notEmpty("setList", userEntry.getSetList());
        UserEntry entry = service.add(userEntry);
        return new ResultUtil<UserEntry>().setData(entry);
    }

    //修改", notes = "修改")
    @PostMapping("/update")
    public Result update(@RequestBody UserEntry userEntry) {
        boolean boo = service.update(userEntry);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    //删除", notes = "删除【逻辑删除】")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean boo = service.delete(id);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

}
